package com.yingxin.yxerp.module.activiti.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.activiti.ApprovalStatus;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.helper.DateHelper;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.activiti.biz.entity.RevokeFinishDateApply;
import com.yingxin.yxerp.module.activiti.biz.mapper.RevokeFinishDateApplyMapper;
import com.yingxin.yxerp.module.activiti.biz.service.IRevokeFinishDateApplyService;
import com.yingxin.yxerp.module.activiti.biz.service.process.RevokeFinishDateApplyProcessHandler;
import com.yingxin.yxerp.module.activiti.biz.vo.resp.RevokeFinishDateApplyRespVo;
import com.yingxin.yxerp.module.common.BusinessTypeHelper;
import com.yingxin.yxerp.module.oplog.aop.OplogAnnotation;
import com.yingxin.yxerp.module.sys.service.IActivitiCoreInformationService;
import com.yingxin.yxerp.module.sys.service.IBusinessTypeSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 完结申请 服务实现类
 * @author fanchao
 * @since 2022-11-15
 */

@Service
public class RevokeFinishDateApplyServiceImpl
        extends ServiceImpl<RevokeFinishDateApplyMapper, RevokeFinishDateApply>
        implements IRevokeFinishDateApplyService {

    @Autowired
    private IUserSupport userSupport;

    @Autowired
    private IBusinessTypeSettingService businessTypeSettingService;

    @Autowired
    private RevokeFinishDateApplyProcessHandler revokeFinishDateApplyProcessHandler;

    @Autowired
    private IActivitiCoreInformationService activitiCoreInformationService;

    @Override
    public RevokeFinishDateApplyMapper mapper() {
        return baseMapper;
    }

    /**
     * 创建
     */
    @OplogAnnotation(biz = BusinessType.WJSQ, id = "{{apply.id}}", action = "新增",
            desc = "申请单号[{{apply.applyNo}}]，业务单号[{{apply.businessNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(RevokeFinishDateApply apply) {
        // 检查是否正在申请中
        QueryWrapper<RevokeFinishDateApply> qw = new QueryWrapper<>();
        qw.lambda().eq(RevokeFinishDateApply::getBusinessId, apply.getBusinessId());
        qw.lambda().eq(RevokeFinishDateApply::getApprovalStatus, ApprovalStatus.PENDING_APPROVAL.getStatus());
        List<RevokeFinishDateApply> list = list(qw);
        if (list.size() > 0) {
            throw new BizException("该单据正在申请完结中，不能重复申请");
        }

        // 创建
        apply.setId(IdHelper.snowflakeId());
        apply.setApplyNo(businessTypeSettingService.next(BusinessType.CXWJSQ));
        apply.setUserId(userSupport.getUserId());
        save(apply);

        // 流程
        revokeFinishDateApplyProcessHandler.startProcessInstance(apply);

        // 塞入app审核列表主要信息
        RevokeFinishDateApplyRespVo vo = getVo( apply.getId() );
        activitiCoreInformationService.create( vo.getId(), BusinessType.WJSQ.getType(), vo );
    }

    /**
     * 根据 id 获取
     */
    @Override
    public RevokeFinishDateApplyRespVo getVo(String id ){
        RevokeFinishDateApplyRespVo vo = baseMapper.getVo( id );
        if( ObjectUtil.isEmpty( vo ) ){
            return null;
        }
        vo.setCreateTimeFormat( DateHelper.dateFormat( vo.getCreateTime(), DateHelper.YYYYMMDDHHMMSS ) );
        vo.setBusinessTypeName( BusinessTypeHelper.getName( vo.getBusinessType() ) );
        return vo;
    }

}
